package com.welsmann.app.start.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.mysql.jdbc.StringUtils;
import com.welsmann.app.start.common.IndexData;
import com.welsmann.app.start.common.StartConst;
import com.welsmann.app.start.entity.SiteType;
import com.welsmann.app.start.service.IWeatherService;

@Controller
public class IndexController extends BaseController{
	
	@Resource(name = "WeatherService")
	private IWeatherService weatherService;
	
	@RequestMapping("/index")
	public ModelAndView index() {
		ModelAndView mav = new ModelAndView("index");
		String holiday = super.getCurrentHoliday();
		if (!StringUtils.isEmptyOrWhitespaceOnly(holiday)) {
			mav.addObject(StartConst.REQUEST_CURRENT_HOLIDAY, holiday);
		}
		//组成首页快捷方式数据
		List<IndexData> lstIndexData = new ArrayList<IndexData>();
		lstIndexData.add(new IndexData(StartConst.REQUEST_SITE_TOP_TITLE, super.siteService.getTopAll()));
		List<SiteType> lstSiteType = super.siteService.getAllSiteType();
		if (lstSiteType != null && lstSiteType.size() > 0) {
			for (SiteType st : lstSiteType) {
				lstIndexData.add(new IndexData(st.getName(), super.siteService.getTopByType(st.getId())));
			}
		}
		mav.addObject(StartConst.REQUEST_SITE_INDEX, lstIndexData);
		mav.addObject(StartConst.SESSION_SITE_AIR_QUALITY_INFO, weatherService.getLatest());
		return mav;
	}
}
